<?php

/**
 * 广告创意管理MODEL类
 * 
 * @author Dean
 *
 */
class adsModel extends Model{
	
	//操作的本类表名
	private $tableName = 'ads';
	private $launchtableName = 'launch';
	private $importlaunchtableName = 'importlaunch';
	
	/**
	 * 查询全部投放信息
	 */
	public function findAll($limit=0, $num=20, $type, $params=array()){
		
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= " AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			return array();
		}
		if (!empty($params['v_activity'])){
			return array();
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_launch_name'])){
			$select_by_sql .= " AND `launch_name` like '%{$params['v_launch_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_weight'])){
			$select_by_sql .= " AND `weight` like '%{$params['v_weight']}%' ";
		}
		if (!empty($params['v_keywords'])){
			return array();
		}
		if (!empty($params['v_num'])){
			return array();
		}
		
		$sql = "select * from ".tblName($this->tableName)."  WHERE op <> 'c' AND type = '$type' $select_by_sql ORDER BY createTime desc LIMIT ".$limit." , ".$num;
		
		try {
			$adsList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsList;
	}
	
	/**
	 * 统计投放总数量
	 * 
	 * @param String $type
	 */
	public function countAll($type){
		
		$sql = 'select count(*) as num from '.tblName($this->tableName)." WHERE op <> 'c' AND type = '$type' ";
		
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	public function countAllByType($type, $params=array()){
	
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= " AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			return 0;
		}
		if (!empty($params['v_activity'])){
			return 0;
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_launch_name'])){
			$select_by_sql .= " AND `launch_name` like '%{$params['v_launch_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_weight'])){
			$select_by_sql .= " AND `weight` like '%{$params['v_weight']}%' ";
		}
		if (!empty($params['v_keywords'])){
			return 0;
		}
		if (!empty($params['v_num'])){
			return 0;
		}
		
		$sql = 'select count(*) as num from '.tblName($this->tableName)." WHERE op <> 'c' AND type = '$type' $select_by_sql";
	
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	/**
	 * 查询单个投放信息，通过投放编号进行查询
	 * 
	 * @param int $id 投放编号
	 */
	public function findOne($id){
		
		$sql = 'select * from '.tblName($this->tableName).' WHERE ID = \''.$id.'\'';
		
		try {
			$adsInfo = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsInfo;
	}
	
	/**
	 * 进行投放新增
	 * 
	 * @param Array $array
	 * @param int $returnID
	 */
	public function add($array, $returnID = 1){
		
		$sql = insertTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0 && $returnID == 1) {
			return $this->db->lastInsertID;
		}else {
			throw new Exception('投放添加失败。');
			return -1;
		}
	}
	
	/**
	 * 进行修改操作
	 * 
	 * @param Array $array
	 * @param Array $where
	 */
	public function modify($array, $where){
		$sql = updateTableSql($this->tableName, $array, $where);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	/**
	 * 进行删除操作
	 * 
	 * @param Array $array
	 */
	public function delete($array){
		
		$sql = deleteTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	public function importDataToDB($fields, $arr){
		
		$sql = "replace into ".tblName($this->tableName)." (".implode(',', $fields).") values ".implode(',', $arr)." ";
		
		
		$numRows = $this->db->execute($sql);
		
		if ($numRows > 0) {
// 			return $this->db->lastInsertID;
			return $numRows;
		}else {
			throw new Exception('创意添加失败。');
			return -1;
		}
	}
	
	public function importDataToDB2($fields, $arr){
	
		$sql = "replace into ".tblName($this->tableName)." (".implode(',', $fields).") values ".$arr." ";
	
		$numRows = $this->db->execute($sql);
	
		if ($numRows > 0) {
			// 			return $this->db->lastInsertID;
			return $numRows;
		}else {
			throw new Exception('创意添加失败。');
			return -1;
		}
	}
	
	public function findVBCAdsByLaunchName($launchName){
		
		$sql = "select * from ".tblName($this->tableName)."  WHERE op <> 'c' AND type = 'vbc' AND name like '{$launchName}%'  ORDER BY createTime desc ";
		
		try {
			$adsList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsList;
	}
	
	public function findAdsByVBCId($id){
	
		$sql = "select * from ".tblName($this->tableName)."  WHERE type = 'vbc' AND launch_name = (select name from ".tblName($this->launchtableName)." where id = '$id' )  ORDER BY createTime desc ";
	
		try {
			$adsList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsList;
	}
	
	public function findAdsByImportVBCId($id){
	
		$sql = "select * from ".tblName($this->tableName)."  WHERE type = 'vbc' AND launch_name = (select name from ".tblName($this->importlaunchtableName)." where id = '$id' )  ORDER BY createTime desc ";
	
		try {
			$adsList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsList;
	}
	
	public function getImportInfoByAdsID($adsID){
		
		$sql = "select t1.*, t2.order, t2.activity from ".tblName($this->tableName)." t1 left join ".tblName($this->importlaunchtableName)." t2 on t2.name=t1.launch_name where t1.id='$adsID' ";
		
		try {
			$adsInfo = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsInfo;
	}
	
	public function findAdsByAdsIDs($ids){
		$sql = "select * from ".tblName($this->tableName)."  WHERE type = 'vbc' AND id in ({$ids})  ORDER BY createTime desc ";
		
		try {
			$adsList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsList;
	}
	
	public function findImportOrderByVBCName($name){
		$sql = "SELECT t1.`order`, t1.activity FROM tbl_importlaunch t1 LEFT JOIN tbl_ads t2 ON t1.name=t2.launch_name WHERE t2.name LIKE '$name%'";
		
		try {
			$List = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $List;
	}
}


?>